K-MEANS
df <- iris
df <- na.omit(df)
df.class <- df[,"Species"]
df <- df[,c(1,2,3,4)]
head(df)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 5.1 3.5 1.4 0.2
## 2 4.9 3.0 1.4 0.2
## 3 4.7 3.2 1.3 0.2
## 4 4.6 3.1 1.5 0.2
## 5 5.0 3.6 1.4 0.2
## 6 5.4 3.9 1.7 0.4
summary(df)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
normalize <- function(x){
return ((x-min(x))/(max(x)-min(x)))
}
df$Sepal.Length <- normalize(df$Sepal.Length)
df$Sepal.Width <- normalize(df$Sepal.Width)
df$Petal.Length <- normalize(df$Petal.Length)
df$Petal.Width <- normalize(df$Petal.Width)
head(df)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 0.22222222 0.6250000 0.06779661 0.04166667
## 2 0.16666667 0.4166667 0.06779661 0.04166667
## 3 0.11111111 0.5000000 0.05084746 0.04166667
## 4 0.08333333 0.4583333 0.08474576 0.04166667
## 5 0.19444444 0.6666667 0.06779661 0.04166667
## 6 0.30555556 0.7916667 0.11864407 0.12500000
result<- kmeans(df, 3)
result$cluster
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
## 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
## 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
## 3 3 3 3 3 3 3 3 3 3 1 2 1 2 2 2 2 2 2 2
## 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2
## 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
## 1 2 1 1 1 1 2 1 1 1 1 1 1 2 1 1 1 1 1 2
## 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
## 1 2 1 2 1 1 2 2 1 1 1 1 1 2 2 1 1 1 2 1
## 141 142 143 144 145 146 147 148 149 150
## 1 1 2 1 1 1 2 1 1 2
result$centers
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 0.7072650 0.4508547 0.79704476 0.82478632
## 2 0.4412568 0.3073770 0.57571548 0.54918033
## 3 0.1961111 0.5950000 0.07830508 0.06083333
result$size
## [1] 39 61 50
par(mfrow=c(2,2), mar=c(5,4,2,2))
plot(df[c(1,2)], col=result$cluster)
plot(df[c(1,2)], col=df.class)
plot(df[c(3,4)], col=df.class)
result$cluster <- as.factor(result$cluster)

library(ggplot2)
ggplot(df, aes(Petal.Length, Petal.Width, color = result$cluster)) + geom_point()

plot(df[c("Sepal.Length", "Sepal.Width")], col=result$cluster)

plot(df[,], col=result$cluster)

library(animation)
km1<-kmeans.ani(df,3)


















library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
fviz_cluster(result, data = df)

k2 <- kmeans(df, centers = 2, nstart = 25)
k3 <- kmeans(df, centers = 3, nstart = 25)
k4 <- kmeans(df, centers = 4, nstart = 25)
k5 <- kmeans(df, centers = 5, nstart = 25)
# plots to compare
p1 <- fviz_cluster(k2, geom = "point", data = df) + ggtitle("k = 2")
p2 <- fviz_cluster(k3, geom = "point", data = df) + ggtitle("k = 3")
p3 <- fviz_cluster(k4, geom = "point", data = df) + ggtitle("k = 4")
p4 <- fviz_cluster(k5, geom = "point", data = df) + ggtitle("k = 5")
library(gridExtra)
grid.arrange(p1, p2, p3, p4, nrow = 2)

DECISION TREES
library(party)
## Loading required package: grid
## Loading required package: mvtnorm
## Loading required package: modeltools
## Loading required package: stats4
## Loading required package: strucchange
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
dtree <- ctree(Species~., data = as.list(iris))
plot(dtree)
